import { defineStore } from 'pinia'
interface Good {
  name: string
  id: number
  total: number
  price: number
  src: string
}
interface CarList {
  carList: Good[]
  money: number
}
export const useCarStore = defineStore('car', {
  state: (): CarList => ({
    carList: [],
    money: 0,
  }),
  actions: {
    addCar(good: Good) {
      const { id } = good
      const existGood = this.carList.find((item: Good) => item.id === id)
      if (existGood) {
        existGood.total += good.total
      } else {
        this.carList.push(good)
      }
      console.log(this.carList)
    },
    delCarItem(id: number) {
      this.carList = this.carList.filter((item: Good) => item.id !== id)
    },
  },
  getters: {
    totalMoney: (state) =>
      state.carList.reduce((total, item) => {
        return total + item.total * item.price
      }, 0),
  },
})
